package work.linruchang.oauthresourceproject.controller;

import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import work.linruchang.oauthresourceproject.bean.SysUser;
import work.linruchang.oauthresourceproject.config.oauth.ThreadLocalRequestInfo;
import work.linruchang.oauthresourceproject.service.SysUserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户信息相关接口
 *
 * @author LinRuChang
 * @version 1.0
 * @date 2022/08/18
 * @since 1.8
 **/
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    SysUserService sysUserService;

    /**
     * 用户页面
     *
     * @return
     */
    @GetMapping
    public String userPage() {
        return "user";
    }

    /**
     * 退出登录
     * 命令浏览器清除当前站点的所有缓存以及cookie信息
     * @return
     */
    @GetMapping("logout")
    @SneakyThrows
    public void logout(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
        httpServletResponse.setStatus(401);
        httpServletResponse.setHeader("Clear-Site-Data", "\"cache\", \"cookies\"");
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("user_info")
    @ResponseBody
    public SysUser getUserInfo() {
        return sysUserService.getById(ThreadLocalRequestInfo.getUserId());
    }




}
